package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Employee;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.text.Utilities;
import java.util.*;
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private  DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;

    public DishServiceImpl(DishMapper dishMapper) {
    }

    public void save(DishDTO dishDTO){
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        dishMapper.insert(dish);

        Long dishId = dish.getId();
        //主键回填后的主键值返回给id并赋值
        List<DishFlavor> flavorList = dishDTO.getFlavors();

        if(flavorList.size()>0){
            flavorList.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.insertBatch(flavorList);
        }
        
    }
    public PageResult pagequery(DishPageQueryDTO dishPageQueryDTO){
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page= dishMapper.pageQuery(dishPageQueryDTO);

        Long total = page.getTotal();
        List<DishVO>  records=page.getResult();
        PageResult pageResult=new PageResult(total,records);
        return pageResult;
    }
    public void delete(List<Long> ids){
        //起售菜品不能删除
        //菜品删除口味对应删除
        //套餐关联的菜品不能删除
        for(Long id:ids){
            if(dishMapper.getStatusById(id).equals(StatusConstant.ENABLE))
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            if(!setMealDishMapper.getByDishId(id).isEmpty()){
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
            //dishMapper.delete(id);
            //dishFlavorMapper.deleteByDishId(id);
        }

        dishMapper.deleteByIds(ids);
        dishFlavorMapper.deleteByDishIds(ids);



    }

    public DishVO getById(Long id){
        DishVO dishVO=new DishVO();
        Dish dish = dishMapper.getById(id);
        if(dish==null)
            throw new AccountNotFoundException(MessageConstant.DISH_NOT_FOUND);

        BeanUtils.copyProperties(dish,dishVO);

        List<DishFlavor> dishFlavor = dishFlavorMapper.getByDishID(id);

        dishVO.setFlavors(dishFlavor);
        return dishVO;
    }

    public void update(DishDTO dishDTO){
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        List<DishFlavor> dishFlavors=dishDTO.getFlavors();
        for(DishFlavor dishFlavor:dishFlavors){
            dishFlavor.setDishId(dishDTO.getId());
        }
        dishMapper.update(dish);
        //TODO 菜品口味的修改或添加实际是全部删除后重新添加，存在缺陷
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        if(!dishFlavors.isEmpty())
            dishFlavorMapper.insertBatch(dishFlavors);

    }
    public void startOrStop(Integer status, Long id){
        //Employee employee=new Employee();
        //employee.setId(id);
        //employee.setStatus(status);

        Dish dish=Dish.builder().id(id).status(status).build();

        dishMapper.update(dish);

    }
    public List<Dish> list(Dish dish){
        List<Dish> dishList = dishMapper.list(dish);
        return dishList;
    }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */

    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishID(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
